package org.redmaple.core.template.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.redmaple.core.bean.ResponseResults;
import org.redmaple.core.template.dto.BaseDTO;
import org.redmaple.core.template.model.BaseModel;
import org.redmaple.core.util.ServletRequestUtils;
import org.springframework.ui.Model;

/**
 * BaseController适配器
 * 
 * @author Xiangyun
 *
 * @param <T> 域模型
 * @param <X> 视图模型对象
 */
public abstract class BaseControllerAdapter<T extends BaseModel, X extends BaseDTO> extends BaseController<T, X> {
	
	/**
	 * 跳转到更新界面时，传递的对象的KEY, 为视图模型对象
	 */
	protected static final String UPDATE_MAP_OBJECT_NAME = "object";
	
	/**
	 * 跳转到查询界面时，传递的对象的KEY, 为视图模型对象
	 */
	protected static final String VIEW_MAP_OBJECT_NAME = "object";
	
	/**
	 * 批量删除请求时，ID列表存放的KEY
	 */
	protected static final String DELETES_PROPERTY_NAME = "ids";
	
	public void openQueryPage(Model model) {
		
	}

	public void openNewPage(Model model) {

	}

	public void openUpdatePage(X objectDTO, Model model) {
		model.addAttribute(UPDATE_MAP_OBJECT_NAME, objectDTO);
	}

	public void openViewPage(X objectDTO, Model model) {
		model.addAttribute(VIEW_MAP_OBJECT_NAME, objectDTO);
	}
	
	protected List<String> deleteEntitiyIds(HttpServletRequest request) {
		return ServletRequestUtils.getStringParamters(request,
				DELETES_PROPERTY_NAME);
	}

	public ResponseResults saveSuccess() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(true);
		return ret;
	}

	public ResponseResults saveFailed() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(false);
		return ret;
	}

	public ResponseResults deleteSuccess() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(true);
		return ret;
	}

	public ResponseResults deleteFailed() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(false);
		return ret;
	}

	public ResponseResults deletesSuccess() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(true);
		return ret;
	}

	public ResponseResults deletesFailed() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(false);
		return ret;
	}

	public ResponseResults updateSuccess() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(true);
		return ret;
	}

	public ResponseResults updateFailed() {
		ResponseResults ret = new ResponseResults();
		ret.setResult(false);
		return ret;
	}
}
